package cn.tedu.erp.project.service.impl;

import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.mapper.CustomSalesMapper;
import cn.tedu.erp.project.pojo.dto.CustomSalesAddNewDTO;
import cn.tedu.erp.project.pojo.dto.CustomSalesUpdateDTO;
import cn.tedu.erp.project.pojo.entity.CustomManage;
import cn.tedu.erp.project.pojo.entity.CustomSales;
import cn.tedu.erp.project.pojo.vo.CustomManageStandardVO;
import cn.tedu.erp.project.pojo.vo.CustomSalesListVO;
import cn.tedu.erp.project.pojo.vo.CustomSalesStandardVO;
import cn.tedu.erp.project.service.ICustomSalesService;
import cn.tedu.erp.project.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CustomSalesServiceImpl implements ICustomSalesService {
    @Autowired
    private CustomSalesMapper customSalesMapper;
    @Autowired
    private CustomSalesServiceImpl customSalesService;
    @Override
    public void addNew(CustomSalesAddNewDTO customSalesAddNewDTO) {
        log.debug("开始处理【添加订单】的业务,参数: {}",customSalesAddNewDTO);
        String name = customSalesAddNewDTO.getOrderId();
        int count = customSalesMapper.countByOrderId(customSalesAddNewDTO.getOrderId());
        log.debug("根据订单编号统计数量:{}",count);
        // 判断统计结果是否大于0
        if (count > 0){
            // -- 是：名称被占用，抛出异常，例如：throw new RuntimeException()
            String message = "此订单已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        // 创建订单对象
        CustomSales customSales = new CustomSales();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到customSales对象中
        BeanUtils.copyProperties(customSalesAddNewDTO,customSales);
        // 调用Mapper对象的insert()执行插入订单数据
        log.debug("插入数据:{}",customSales);
        customSalesMapper.insert(customSales);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除订单】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        CustomSalesStandardVO customSales=customSalesMapper.getStandardById(id);
        log.debug("根据ID={}检查订单数据是否存在，查询结果：{}", id, customSales);
        // 判断查询结果是否为null
        if (customSales == null) {
            // 是：数据不存在，抛出异常
            String message = "删除合同失败，尝试删除的订单数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        customSalesMapper.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, CustomSalesUpdateDTO customSalesUpdateDTO) {
        log.debug("开始处理【修改订单详情】的业务，ID：{}，新数据：{}", id, customSalesUpdateDTO);
        // 调用Mapper对象的getStandardById()执行查询
        CustomSalesStandardVO queryResult = customSalesMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改订单详情失败，尝试修改的订单数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 创建WareHouseCategory对象
        CustomSales customSales=new CustomSales();
        // 复制属性，设置ID
        BeanUtils.copyProperties(customSalesUpdateDTO, customSales);
        customSales.setId(id);
        // 调用Mapper对象的update()方法执行修改
        customSalesMapper.update(customSales);
    }

    @Override
    public CustomSalesStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询订单详情】的业务，参数：{}", id);

        CustomSalesStandardVO customSales= customSalesMapper.getStandardById(id);
        if (customSales == null) {
            // 是：抛出异常
            String message = "查询订单详情失败，订单数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return customSales;
    }

    @Override
    public List<CustomSalesListVO> list() {
        log.debug("开始处理【订单列表】的业务,无参数");
        List<CustomSalesListVO> list = customSalesMapper.list();
        return list;
    }

    @Override
    public void rebuildCache() {

    }
}
